package com.spc.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.csy.httpApiDemo.AccountInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ktt.bean.Bis_custom_user;
import com.ktt.service.Bis_custom_userService;
import com.spc.bean.Bis_wechat_record;
import com.spc.service.Bis_wechat_recordService;
import com.spc.util.WechatUtil;

@Controller
@RequestMapping("biswechat")
public class Bis_wechat_recordController {
 
	@Autowired
	private Bis_wechat_recordService bis_wechat_recordService;
	@Autowired
	private Bis_custom_userService bis_custom_userService;
	
	@RequestMapping("/list")
	public String list(Model model,HttpServletRequest request,@RequestParam(value="page",defaultValue="1")int page){
		Map<String, Object> map = initMap(request);
		PageHelper.startPage(page, 4);
		List<Bis_wechat_record> list=this.bis_wechat_recordService.listAll(map);
		PageInfo<Bis_wechat_record> info=new PageInfo<>(list);
		model.addAttribute("info",info);
		return "spc/Bis_wechat_recordList";
	}
	
	private Map<String, Object> initMap(HttpServletRequest request) {
		String username=request.getParameter("username");//收件人
		String mobile=request.getParameter("mobile");//收件人账号
		String content=request.getParameter("content");//内容
		String send_time1=request.getParameter("send_time1");//发起时间1
		String send_time2=request.getParameter("send_time2");//发起时间2
	    Map<String, Object> map=new HashMap<String,Object>();//map集合存值
		map.put("username",username );//收件人
		map.put("mobile",mobile );//收件人账号
		map.put("content",content );//内容
		map.put("send_time1",send_time1 );//发起时间1
		map.put("send_time2",send_time2 );//发起时间2
		return map;
	}
	
	@RequestMapping("delete")
	public String delete(String ids){
		bis_wechat_recordService.delete(ids);
		return "redirect:/biswechat/list";
	}
	
	@RequestMapping("toadd")
	public String list(Model model){
//		List<Bis_custom_user> list = this.bis_custom_userService.listAll(new HashMap<>());
//		model.addAttribute("list",list);
		return "spc/Bis_wechat_recordadd";
	}
	
	@RequestMapping("list2")
	public String list2(Model model,HttpServletRequest request){
		String mobile=request.getParameter("mobile");
		String qname=request.getParameter("username");
		Map map=new HashMap<>();
		map.put("mobile", mobile);
		map.put("qname", qname);
		List<Bis_custom_user> list=bis_custom_userService.listAll(map);
		model.addAttribute("list", list);
		return "spc/Bis_wechat_recordaddList";
	}
	@RequestMapping("back")
	public String back(){
		return "spc/Bis_wechat_recordadd";
	}
	
	/*@RequestMapping("save")
	public String save(HttpServletRequest request,Bis_wechat_record wechat_record){
		wechat_record.setRecord_id(UUID.randomUUID().toString());
		String mobile=request.getParameter("mobile");
		String content=request.getParameter("content");
		wechat_record.setRecord_type("2");
			 try{
				// 获取开发者账号信息
				 AccountInfo.execute();
				// 验证码通知短信接口   手机号和发送短信的内容
				 com.spc.miaodi.IndustrySMS.execute(mobile, content);
				 wechat_record.setStatus("1");
			 }catch(Exception e){
				 wechat_record.setStatus("0");
				 e.printStackTrace();
			 }
		this.bis_wechat_recordService.save(wechat_record);
		return "redirect:/biswechat/list";
	}*/
	@RequestMapping("add")
	public String select(String custom_user_id1,Model model){
		System.out.println(custom_user_id1);
		 String[] data = custom_user_id1.split(",");
		 String openids="";
		for (String  custom_user:data) {
		      Bis_custom_user bis_custom_user= bis_custom_userService.getById(Integer.valueOf(custom_user));
		      openids+=bis_custom_user.getMobile()+",";
			}
		if(openids.length()>0){
			openids=openids.substring(0,openids.length()-1);
		}
		model.addAttribute("openids",openids);
		model.addAttribute("userids",custom_user_id1);
		return "spc/Bis_wechat_recordadd";
	}
	
	@RequestMapping("select")
	@ResponseBody
	public Object select(String custom_user_id){
		System.out.println(bis_custom_userService.getById(Integer.valueOf(custom_user_id)));
		return bis_custom_userService.getById(Integer.valueOf(custom_user_id));
		
	}
	
			//发到信息表里面
	@RequestMapping("save")
	public String saveBis_wechat_record(HttpServletRequest request,Bis_wechat_record wechat_record){
		String send_user_id=request.getParameter("mobile");
		System.out.println(send_user_id+"..................................");
		String mobile=request.getParameter("openid");//手机号
		String openid=mobile +",";
		String content=request.getParameter("content");//内容
		String[] openid2=openid.split(",");//去掉，
		String id =send_user_id+",";
		String [] sendid=id.split(",");
		List<String> list=new ArrayList<String>();//集合
		for (String open:openid2) {//遍历数组循环取值
			if(Tools.isNotNull(open)){
				list.add(open);
			}
		}
		// 发送到微信
		WechatUtil.sendTextMessage(list, content);
		for(String send:sendid){
			if(Tools.isNotNull(send)){
				wechat_record.setRecord_id(UUID.randomUUID().toString());
				wechat_record.setSend_user_id(send);
				wechat_record.setStatus("1");
				this.bis_wechat_recordService.save(wechat_record);
			}
		}
		
		return "redirect:/biswechat/list";
	}
			
} 
